from django.db import models

# Create your models here.
# python manage.py makemigrations
# python manage.py migrate

class Book(models.Model):
    title = models.CharField(max_length=32)
    price = models.DecimalField(max_digits=5, decimal_places=2)
    pub_date = models.DateField()
    publish = models.ForeignKey("Publish", on_delete=models.CASCADE)
    # 自动创建book_authors表格
    authors = models.ManyToManyField("Author")

    class Meta:
        db_table = "book"

class Publish(models.Model):
    name = models.CharField(max_length=32)
    city = models.CharField(max_length=64)
    email = models.EmailField()

    class Meta:
        db_table = "publish"

class Author(models.Model):
    '''
    外键在一对多的多中设置：models.ForeignKey("关联类名", on_delete=models.CASCADE)
    (1)、on_delete = None：
    删除关联表的数据时，当前表与关联表的filed的行为。
    (2)、on_delete = models.CASCADE：
    表示级联删除，当关联表（子表）中的数据删除时，与其相对应的主表中的数据也删除。
    (3)、on_delete = models.DO_NOTHING:
    你删你的，外键不想管你
    (4)、on_delete = models.PROTECT:
    保护模式，如采用这个方法，在删除关联数据时会抛出ProtectError错误
    (5)、on_delete = models.SET_DEFAULT:
    设置默认值，删除子表字段时，外键字段设置为默认值，所以定义外键的时候注意加上一个默认值。
    (6)、on_delete = models.SET（值）:
    删除关联数据时，自定义一个值，该值只能是对应指定的实体
    '''
    name = models.CharField(max_length=32)
    age = models.SmallIntegerField()
    au_detail = models.OneToOneField("AuthorDetail", on_delete=models.CASCADE)

    class Meta:
        db_table = "author"

class AuthorDetail(models.Model):
    '''
    OneToOneField = ForeignKey(...，unique=True)设置一对一。
    '''
    gender_choices = (
        (0, "女"),
        (1, "男"),
        (2, "保密"),
    )
    gender = models.SmallIntegerField(choices=gender_choices)
    tel = models.CharField(max_length=32)
    addr = models.CharField(max_length=64)
    birthday = models.DateField()

    class Meta:
        db_table = "author_detail"

'''
INSERT INTO publish(name,city,email) VALUES("人民出版社","北京","renmin@cn.com"),("珠海出版社","珠海","zhuhai@cn.com");
INSERT INTO author_detail(gender,tel,addr,birthday) VALUES(1,"1008866","北京","1990-02-06"),(1,"2008600","上海","1995-04-02"),(2,"3008600","青海","1972-06-25")
INSERT INTO author(name,age,au_detail_id) VALUES("子美",31,1),("清风",26,2),("多隆",49,3)
'''